package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostActivity;
import com.yanqu.road.server.manager.cross.battle.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = CrossProtocol.C2_GHOST_ACTIVITY_CROSS_EXPLORE, desc = "跨服处理探索")
public class CrossGhostExploreCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private PlayerProto.PlayerBaseTempMsg playerBaseTempMsg;
    private long userResetTime;
    private int exploreTimes = 0;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.CrossExploreReqMsg reqMsg = GhostActivityProto.CrossExploreReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        playerBaseTempMsg = reqMsg.getPlayer();
        userResetTime = reqMsg.getUserResetTime();
        if (reqMsg.hasExploreTimes()) {
            exploreTimes = reqMsg.getExploreTimes();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GhostActivity activity = GhostMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        ret = activity.explore(userId, playerBaseTempMsg, userResetTime, exploreTimes);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            GhostActivityProto.CrossExploreRespMsg.Builder builder = GhostActivityProto.CrossExploreRespMsg.newBuilder();
            builder.setRet(ret);
            builder.setUserResetTime(userResetTime);
            MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_GHOST_EXPLORE_FROM_CROSS, builder));
        }
    }
}
